//
//  EFEventPromotionViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/6/10.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFEventPromotionViewController.h"
#import "EFSearchResultCell.h"
#import "EFGoodsDetailViewController.h"
#import "EFSepecialItem.h"
#import "EFEventPromotionItem.h"
#import "EFUserLocationItem.h"

@interface EFEventPromotionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *dataArray;//不同的页面下换dataArray展示即可
@property (strong, nonatomic) UITableView *tableView;//每个对应一个
@property (strong, nonatomic) UIImageView *bannerImageV;

@property (strong, nonatomic) EFEventPromotionItem *promotionItem;
@property (strong, nonatomic) NSNumber *eventId;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) NSInteger size;
@end

@implementation EFEventPromotionViewController

- (instancetype)initWithEventId:(NSNumber *)eventId{
    self = [super init];
    if (self) {
        self.title = @"专题";
        self.eventId = eventId;
        self.page = 1;
        self.size = 20;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.bannerImageV];
    [self.view addSubview:self.tableView];
     __weak typeof(self) weakSelf = self;
    // Do any additional setup after loading the view.
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf.dataArray removeAllObjects];
         weakSelf.page = 1;
        [weakSelf requestContent];
       
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (weakSelf.dataArray.count >= weakSelf.promotionItem.productTotal.integerValue) {
            [weakSelf.tableView.footer endRefreshing];
            return ;
        }
        weakSelf.page ++;
        [weakSelf requestContent];
    }];
    [self requestContent];
}

- (void)requestContent{
    __weak typeof(self) weakSelf = self;
    [self showHUD];
    //组装position
    CLLocationCoordinate2D coor = APP_CONTEXT.locationManager.pinLocation.userCoordinate;
    NSString *str = [NSString stringWithFormat:@"%f,%f",coor.longitude,coor.latitude];
    [APP_CONTEXT.net getActivityList:self.eventId position:str page:[NSNumber numberWithInteger:self.page] size:[NSNumber numberWithInteger:self.size] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.HUD.hidden = YES;
        if (code == 0) {
            weakSelf.promotionItem = [EFEventPromotionItem yy_modelWithDictionary:info];
            [weakSelf.bannerImageV setImageWithURL:[NSURL URLWithString:weakSelf.promotionItem.bannerImg]];
            if (weakSelf.page == 1) {
                weakSelf.dataArray = self.promotionItem.productList;
            }else{
                [weakSelf.dataArray addObjectsFromArray:self.promotionItem.productList];
            }
            [weakSelf.tableView reloadData];
        }
    }];
}


#pragma mark - TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 102;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    if (tableView == self.tableView && self.dataArray.count > 0 && indexPath.row < self.dataArray.count) {
        NSObject *item = self.dataArray[indexPath.row];
        ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search"];
        if (cell == nil){
            cell = [[EFSearchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"search"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [(EFSearchResultCell *)cell setObject:(EFSepecialItem *)item];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // ICTableViewItem *item = self.dataArray[indexPath.row];
    if(tableView == self.tableView){
        EFSepecialItem *item = self.dataArray[indexPath.row];
        //跳转商品详情页
        EFGoodsDetailViewController *vc = [[EFGoodsDetailViewController alloc] initWithProductId:item.id wareHouseId:item.warehouse_id];
        [ICNavigator pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 164, kScreenWidth, kScreenHeight - 64 - 164) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (UIImageView *)bannerImageV{
    if (!_bannerImageV) {
        _bannerImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 164)];
        _bannerImageV.backgroundColor = kGrayColor;
        _bannerImageV.contentMode = UIViewContentModeScaleAspectFill;
        _bannerImageV.clipsToBounds = YES;
    }
    return _bannerImageV;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
